define(function (require) {
    require('../../../c/base/index');
    require('../../../c/nav/index');

    require('../../../c/pagination/index');

    require('./categoryGoods-list-tpl');
    var categoryGoodsTpl = juicer.templates['categoryGoods-list.juicer.html'];

    var categoryGoods = {
        init: function () {
            //初始化菜单选中效果
            $(".fa-circle-o").each(function () {
                if ($(this).parents("a").text() == "外店类目管理") {
                    $(this).parents("a").parents("li").addClass("active");
                }
            });
            var categoryId = CRM.getUrlParam('actcatId');
            var catName = CRM.getUrlParam('actcatName');
            var cityCodeBack = CRM.getUrlParam('cityCode');
            $("#back").attr('data-city',cityCodeBack);
            $("#categoryId").val(categoryId);//保存跳转来的类目id
            $("#catName").text(catName);
            //搜索对应类目的商品信息并渲染
            this.doSearch();
            //绑定事件
            this.bindEvent();
        },
        /*
        解绑全部商品
         */
        unBindAllGoods: function () {
            var self = this;
            var isp = $("#platformTags").find('option:selected').attr('data-id');
            if (isp == ""){
                CRM.showDialog("请先在下拉框选择对应的外部平台");
            } else {
                CRM.request({
                    "type": "GET",
                    "url": CRM.getOuterUrl('/outway/actcat/unbanding/all'),
                    "dataType": "json",
                    "data": {
                        "actcatId": $("#categoryId").val(),
                        //"isp": $("#platformTags button.active").data('id')
                        "isp": isp
                    },
                    "success": function (req) {
                        CRM.showDialog(req.message);
                        self.doSearch();
                    },
                    "error": function (req) {
                        CRM.showDialog(req.message);
                    }
                });
            }
        },
        /*
        解绑列表商品
         */
        unBandGoods: function (id) {
            var self = this;
            CRM.request({
                "type": "GET",
                "url": CRM.getOuterUrl('/outway/actcat/unbanding'),
                "dataType": "json",
                "data": {
                    "id": id
                },
                "success": function (req) {
                    CRM.showDialog(req.message);
                    self.doSearch();
                },
                "error": function (req) {
                    CRM.showDialog(req.message);
                }
            });
        },
        /*
        操作确认
         */
        showConfirm: function (type,id,name) {
            $("#unBindType").attr('data-type',type);
            $("#unBindType").val(id);
            console.log(name);
            $("#goodNameInfo").text(name);
            $("#confirm_dialog").modal('show');
        },
        /*
        商品搜索
         */
        doSearch: function (pageNo) {
            var self = this;
            var pageNo = (pageNo == null) ? 1 : pageNo;
            var actCatId = $("#categoryId").val();
            //var isp = $("#platformTags button.active").data('id');
            var isp = $("#platformTags").find('option:selected').attr('data-id');
            var shopId = $("#shopId").val().trim();
            if (/^(|\d+)$/.test(shopId)){
                CRM.request({
                    "type": "GET",
                    "dataType": "json",
                    "url": CRM.getOuterUrl('/outway/actcat/page'),
                    "data": {
                        "actcatId": actCatId,
                        "barcode": $("#goodsCode").val(),
                        "isp": isp,
                        "pageNo": pageNo,
                        "pageSize": 20,
                        "shopId": shopId,
                        "name": $("#goodsName").val(),
                        "t": (new Date()).getTime()
                    },
                    "success": function (req) {
                        if (req.status){
                            if (req.entry.totalCount > 0){
                                self.renderSearchList(req.entry.result,req.entry.totalCount,req.entry.pageNo);
                            }else {
                                var noResult = '<tr><td valign="top" colspan="7" style="text-align: center;font-size: 18px;">没有匹配的结果</td> </tr>';
                                $("#goodsTBody").html(noResult);
                                // self.renderSearchList(req.entry.result,req.entry.totalCount,req.entry.pageNo);
                                // CRM.showDialog("没有匹配的结果");
                            }

                        }else {
                            CRM.showDialog(req.message);
                        }
                    },
                    "error": function (req) {
                        CRM.showDialog(req.message);
                    }
                });
            }else {
                CRM.showDialog("商品ID只能为数字格式");
            }

        },
        /*
        渲染列表及分页器
         */
        renderSearchList: function (result,total,pageNo) {
            var self = this;
            //console.log(result);
            var html = categoryGoodsTpl({data:result});
            console.log(html);
            $("#goodsTBody").html(html);
            self.initPagination(total,pageNo);
        },
        /*
         初始化分页器
         */
        initPagination: function (total,currentPage) {
            //alert("init");
            console.log(total);
            var self = this;
            var times = 1;
            var total = (total == 0) ? 1 :total;
            $('.pagination').html('<div id="pagination"></div>');
            $('#pagination').twbsPagination({
                totalPages: Math.ceil(total/20) ,
                visiblePages: 7,
                startPage: currentPage,
                onPageClick: function (event, page) {
                    if (times > 1){
                        self.doSearch(page);
                    }
                    times ++;
                }
            });
        },
        bindEvent: function () {
            var self = this;
            //返回上页
            $("#back").on('click',function () {
                location.href = CRM.getOuterUrl('/crm/www/outflat/category.html') + "?cityCode=" + $("#back").attr('data-city');
            });
            //平台按钮切换查询
            //$("#platformTags button").on('click',function (e) {
            $("#platformTags").on('change',function (e) {
                // var target = $(e.currentTarget);
                // $("#platformTags button").removeClass('active');
                // target.addClass('active');
                self.doSearch();
            });
            //添加商品页面跳转
            $("#toAddGoodsPage").on('click',function () {
                location.href = CRM.getOuterUrl('/crm/www/outflat/addGoods.html') + "?actcatId=" + $("#categoryId").val() + "&actcatName=" +$("#catName").text() + "&cityCode=" + $("#back").attr("data-city");
            });
            //全部解绑商品按钮 -> 弹出确认操作浮层
            $("#unbindAllGoods").on('click',function () {
                var type = 'all';
                var id = '';
                self.showConfirm(type,id);
            });
            //解绑列表商品 -> 弹出确认操作浮层
            $("#goodsTBody").on('click','.unBindForList',function (e) {
                var target = $(e.currentTarget);
                var type = 'list';
                var id = target.attr('data-id');
                var goodName = target.attr('data-name');
                self.showConfirm(type,id,goodName);
            });
            //确认操作浮层确认解绑
            $("#confirm").on('click',function () {
                var type = $("#unBindType").attr('data-type');
                var id = $("#unBindType").val();
                if (type == 'all'){
                    self.unBindAllGoods();
                } else if (type == 'list'){
                    self.unBandGoods(id);
                }
                $("#confirm_dialog").modal("hide");
            });
            //参数查询
            $("#search_goods").on('click',function () {
                self.doSearch();
            });
        }
    };

    categoryGoods.init();
});